package com.github.niefy.modules.wx.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.niefy.common.utils.RestTemplateUtils;
import com.github.niefy.modules.wx.entity.ChangduUserRecharge;
import com.github.niefy.modules.wx.entity.WeiboCallMe;
import com.github.niefy.modules.wx.manage.WeiBoManageController;
import com.github.niefy.modules.wx.service.WeiboCallMeService;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class WeiBoServiceImpl {
    @Autowired
    private WeiboCallMeService weiboCallMeService;

//    public void callWeiBo_shijian() {
//        long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
//
//        Map<String, String> paramMap = new HashMap<>();
//        paramMap.put("IMP", "6CE7D5AF23EDB7B445FA8BF79E193DA2A6D96155ED3570391FACBFB0BD0746B907D440729543679FD9AD807BA6609BB937B6CAF121CFAB89C4293AD971AE3745ACAAD7B35DC632D01D94E16D3278E8CBD64073BFE35FD92C504EA72D3D0BA60562D4A07EB50CD0A72D3A2E9A6ECE6E6982BBACFB6ACEEB298500255323F42503554D74AE76481632EADEA5DBC1B5AD889D32A984807339CF5262463B68C21FD2C2FC482CEF35A3D57CBB7AC796B4B333B117E4B9DD40620E1A0EFFFE6E57BB029A7EAC89F81EC239C8ED2EFDB1FC632AFAF6D9ACCD2437E38D590808126C8C3802773BB6D69363C894415E996E117278632ECD252C16178FD1F87B2FA13BFBC9795F6F86A0C74914DF8AE6BEF29689B3");
//        paramMap.put("action_type", "5");
//        paramMap.put("active_time", String.valueOf(epochMilli));
//
//        String withParams = RestTemplateUtils.getWithParams("http://appmonitor.biz.weibo.com/sdkserver/active", String.class, paramMap);
//        System.out.println(withParams);
//    }

    /**
     * 回调微博的事件
     * <p>
     * 关注
     */
    public void wb_JianCeLianJie_CallWeiBo_guang_zhu() {
        try {
            WeiboCallMe byNewOne = weiboCallMeService.getByNewOne();

            long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("IMP", byNewOne.getImp());
            paramMap.put("action_type", "5");
            paramMap.put("active_time", String.valueOf(epochMilli));

            String withParams = RestTemplateUtils.getWithParams("http://appmonitor.biz.weibo.com/sdkserver/active", String.class, paramMap);
            System.out.println("callWeiBo_shijian = " + withParams);

            // 不能用重复的东西，去转发告诉微博
            weiboCallMeService.disableService(byNewOne.getId());
        } catch (Exception e) {
            log.error("WeiBoServiceImpl wb_JianCeLianJie_CallWeiBo_guang_zhu 调用微博失败 e={}", e.getMessage(), e);
        }
    }

    /**
     * 回调微博的事件
     * <p>
     * 关注
     */
    public void test_wb_JianCeLianJie_CallWeiBo_fu_fei() {
        try {
            WeiboCallMe byNewOne = weiboCallMeService.getByNewOne();

            long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

            Map<String, String> paramMap = new HashMap<>();
            paramMap.put("IMP", byNewOne.getImp());
            paramMap.put("action_type", "6");
            paramMap.put("active_time", String.valueOf(epochMilli));

            String withParams = RestTemplateUtils.getWithParams("http://appmonitor.biz.weibo.com/sdkserver/active", String.class, paramMap);
            System.out.println("test_wb_JianCeLianJie_CallWeiBo_fu_fei = " + withParams);

            // 不能用重复的东西，去转发告诉微博
            weiboCallMeService.disableService(byNewOne.getId());
        } catch (Exception e) {
            log.error("WeiBoServiceImpl wb_JianCeLianJie_CallWeiBo_guang_zhu 调用微博失败 e={}", e.getMessage(), e);
        }
    }

    public void wb_JianCeLianJie_CallWeiBo_fu_fei(List<ChangduUserRecharge> userRechargeList) {
        if (ObjectUtils.isEmpty(userRechargeList)) {
            return;
        }
        log.info("wb_JianCeLianJie_CallWeiBo_fu_fei 1111 = " + JSON.toJSONString(userRechargeList));
        List<String> ipList = userRechargeList.stream().map(ChangduUserRecharge::getIp).collect(Collectors.toList());
        List<WeiboCallMe> weiboCallMeList = weiboCallMeService.listByIp(ipList);
        weiboCallMeList.sort(Comparator.comparing(WeiboCallMe::getId).reversed());
        WeiboCallMe weiboCallMe = weiboCallMeList.get(0);
        ChangduUserRecharge changduUserRecharge = userRechargeList.get(0);

        long epochMilli = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();

        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("IMP", weiboCallMe.getImp());
        paramMap.put("action_type", "6");
        BigDecimal payFee = changduUserRecharge.getPayFee();
        BigDecimal divide = payFee.divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        paramMap.put("price", divide.toString());
        paramMap.put("active_time", String.valueOf(epochMilli));

        String withParams = RestTemplateUtils.getWithParams("http://appmonitor.biz.weibo.com/sdkserver/active", String.class, paramMap);
        log.info("wb_JianCeLianJie_CallWeiBo_fu_fei 2222 = " + withParams);
    }

    public void wb_JianCeLianJie_CallMe(WeiBoManageController.WbCallMe wbCallMe) {
        WeiboCallMe weiboCallMe = wbCallMe.toWeiboCallMe();
        weiboCallMeService.saveService(weiboCallMe);
    }

    @Getter
    public enum BehaviorCodeEnum {
        FORM_SUBMISSION(1001, "表单提交"), PHONE_CALL(1002, "电话拨打"), EFFECTIVE_CONSULTATION(1003, "有效咨询"), WECHAT_COPY(1004, "微信复制"), LANDING_PAGE_VISIT(1005, "落地页访问"), DOWNLOAD_START(1006, "下载开始"), PRODUCT_PURCHASE(1007, "商品购买"), OTHER(1100, "其他"), FINANCIAL_DOCUMENT_COMPLETE(1024, "金融-完件"), FINANCIAL_CREDIT(1025, "金融-授信"), FINANCIAL_LOAN(1026, "金融-放款"), PAYMENT_ORDER_ITEM_COUNT(2001, "付款订单商品数"), ACTIVATION_MINI_PROGRAM(3001, "激活(小程序)"), REGISTER_MINI_PROGRAM(3002, "注册(小程序)"), PAY_MINI_PROGRAM(3003, "付费(小程序)"), WECHAT_FOLLOW(3004, "微信关注"), WECHAT_PAYMENT(3005, "微信付费（非APP）");

        private final int code;
        private final String description;

        BehaviorCodeEnum(int code, String description) {
            this.code = code;
            this.description = description;
        }

        // 根据行为码获取枚举值
        public static BehaviorCodeEnum getByCode(int code) {
            for (BehaviorCodeEnum behaviorCode : values()) {
                if (behaviorCode.getCode() == code) {
                    return behaviorCode;
                }
            }
            throw new IllegalArgumentException("Invalid BehaviorCode: " + code);
        }
    }

}
